/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.websocket;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.web.jdbcbase.service.MenuService;
import com.web.jdbcyw1.module.User;

/**
 *
 * @author 
 */
@Controller
public class SystemWebSocketHandler implements WebSocketHandler {
	static Map<String,String> map = new HashMap<String,String>();
	static{
		 String key0099 ="[{id:\"id1\",text:\"商业1#\",attributes:{type:\"01\",parentId:\"-1\"},children:["+
		"{id:\"id011\",text:\"通道1\",attributes:{type:\"02\",parentId:\"id1\"},children:["+
		"{id:\"id021\",text:\"设备1\",attributes:{type:\"03\",parentId:\"id011\"}},"+
		"{id:\"id022\",text:\"设备2\",attributes:{type:\"03\",parentId:\"id011\"}},"+
		"{id:\"id023\",text:\"设备3\",attributes:{type:\"03\",parentId:\"id011\"}}]},"+
		"{id:\"id012\",text:\"通道2\",attributes:{type:\"02\",parentId:\"id1\"}}]},"+
		"{id:\"id3\",text:\"创意1#\",attributes:{type:\"01\",parentId:\"-1\"}}]";
		 String key0003 = "[{name:\"站id\",field:\"distId\"},{name:\"站名称\",field:\"distName\"},{name:\"运维公司\",field:\"companyName\"}]";
		 String key0005 = "[{name:\"设备id\",field:\"bayId\"},{name:\"设备名称\",field:\"bayName\"},{name:\"设备类型\",field:\"type\"}]";
		 String key0006 = "[{name:\"复选框\",title:\"b\",checkbox:true},{name:\"测点id\",field:\"devId\"},{name:\"测点名称\",field:\"devName\"},{name:\"测点值\",field:\"value\",flag:true}]";
		 String key0007 = "[{name:\"复选框\",title:\"b\",checkbox:true},{name:\"测点id\",field:\"devId\"},{name:\"yx测点名称\",field:\"devName\"},{name:\"yx测点值\",field:\"value\"}]";
		 String key0012 = "[{bayId:\"id1\",bayName:\"变压器\",type:\"01\"},{bayId:\"id2\",bayName:\"开关\",type:\"02\"}]";
		 String key0009 = "[{distId:\"distId\",distName:\"商业1#\",companyName:\"城乡世纪广场\"}]";
		 String key0014 = "[{devId:\"id14\",devName:\"a相电流4\",value:\"41.2\"},{devId:\"id24\",devName:\"b相电流4\",value:\"42.5\"}]";
		
		 String key0015 = "[{devId:\"id1\",devName:\"a相电流\",value:\"1.2\"},{devId:\"id2\",devName:\"b相电流\",value:\"2.5\"}";
		 
		 for(int i=0;i<15000;i++){
			 key0015 += ",{devId:\"id2\",devName:\"b相电流\",value:\""+i+"\"}";
		 }
		 key0015 += "]";
		 String key0017 = "[{devId:\"yxid1\",devName:\"a相电流\",value:\"1.2\"},{devId:\"yxid2\",devName:\"b相电流\",value:\"2.5\"}]";
		 map.put("0099", key0099);
		 map.put("0003", key0003);
		 map.put("0005", key0005);
		 map.put("0006", key0006);
		 map.put("0007", key0007);
		 map.put("0009", key0009);
		 map.put("0012", key0012);
		 map.put("0014", key0014);
		 map.put("0015", key0015);
		 map.put("0017", key0017);
	}
    @Override
    public void afterConnectionEstablished(final WebSocketSession wss) throws Exception {
        System.out.println("连接成功......");
        
        //连接成功便启动线程 每间隔10s发送消息
//        Thread t = new Thread(){
//        	public void run() {
//        		
//        		while(true){
//        			try {
//        				Thread.sleep(10000);  
//        				wss.sendMessage(new TextMessage("服务器发送连接成功消息"));
//        				System.out.println("==============================");
//						if(!wss.isOpen()){
//							break;
//						}
//        			} catch (Exception e) {
//        			}
//        			super.run();
//        		}
//        	}
//        };
//        t.start();
    }

    @Override
    public void handleMessage(WebSocketSession wss, WebSocketMessage<?> wsm) throws Exception {
//    	List<User> list = new ArrayList<User>();
//    	MenuService menuService = (MenuService)ContextLoader.getCurrentWebApplicationContext().getBean("menuService");
//    	list = menuService.queryUser();
    	String str = wsm.getPayload().toString();
    	Gson gson = new Gson(); 
    	JsonObject jsonObject = gson.fromJson(str, JsonObject.class);
    	String key = jsonObject.get("type").getAsString()+jsonObject.get("subType").getAsString();
    	jsonObject.addProperty("data", map.get(key));
    	System.out.println("$$$$$$$$$%%%%^^^^^^^^^^&&&&&&&&*********"+str);
    	System.out.println(key+"=="+jsonObject.toString());
        TextMessage returnMessage = new TextMessage(jsonObject.toString());
		wss.sendMessage(returnMessage);
    }

    @Override
    public void handleTransportError(WebSocketSession wss, Throwable thrwbl) throws Exception {
        if(wss.isOpen()){
            wss.close();
        }
       System.out.println("连接关闭");
    }

    @Override
    public void afterConnectionClosed(WebSocketSession wss, CloseStatus cs) throws Exception {
        System.out.println("连接关闭");
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
    
}
